/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import OBJECT.KhachHang;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import util.Global;


/**
 *
 * @author TAN
 */
public class KhachHangDao {
    private Connect connect=null;

    public KhachHangDao(String User ,String Pass,String port,String DataBase,String Host) {
        connect =new Connect( User , Pass, port, DataBase, Host);
    }
    public String ThemKhachHang(KhachHang kh)
    {   String Loi="";
        Connection conn=connect.GetConnect();
        try {
            CallableStatement cals=conn.prepareCall("{call sp_ThemKhachHang(?,?,?,?,?,?,?)}");
            cals.setString(1, kh.getTen());
             cals.setString(2, kh.getDiaChi());
              cals.setString(3, kh.getDienThoai());
              cals.setDate(4,new java.sql.Date(kh.getNgaySinh().getTime()));
               cals.setString(5, kh.getUserName());
               cals.setString(6, kh.getPassWord());
               
               cals.registerOutParameter(7, java.sql.Types.NVARCHAR);
               cals.execute();
               Loi=cals.getString(7);
                  
        } catch (Exception ex) {
            Loi="Falied";
            
        }
        finally{
        CloseConnect();
        }
       
        return Loi;
    }
    
    public String DangNhap(String userName, String password){
        String err = "";
        CallableStatement cals = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = connect.GetConnect();
//            conn = util.Connection.getConnection();
            cals = conn.prepareCall("{call sp_DangNhap(?,?,?,?)}");
            cals.setString("Username", userName);
            cals.setString("Password", password);
            cals.setInt("Type", 0);
            cals.registerOutParameter("err", Types.NVARCHAR);
            cals.setString("err", err);
            rs = cals.executeQuery();
            while(rs.next()){
                Global.customer = new KhachHang();
                Global.customer.setMaKh(rs.getInt("Ma_KH"));
                Global.customer.setTen(rs.getString("Ten_KH"));
                Global.customer.setDiaChi(rs.getString("DiaChi"));
                Global.customer.setDiaChi(rs.getString("DiaChi"));
                Global.customer.setNgaySinh(rs.getDate("NgaySinh"));
                Global.customer.setUserName(userName);
                Global.customer.setPassWord(password);
                Global.customer.setTinhTrang(rs.getString("TinhTrang"));
            }
            
        } catch (Exception e) {
            err = e.getMessage();
            System.err.println(err);
        }finally{
            try {
                err = cals.getString("err");
                if(rs != null){
                    rs.close();
                }
                cals.close();
                CloseConnect();
            } catch (Exception e) {
                
            }
        }
        return err;
    }
    
    public boolean SuaKhachHang(KhachHang kh)
    {
        Connection conn=connect.GetConnect();
        try {
            CallableStatement cals=conn.prepareCall("{call sp_SuaKhachHang(?,?,?,?,?,?,?)}");
            cals.setInt(1, kh.getMaKh());
            cals.setString(2, kh.getTen());
             cals.setString(3, kh.getDiaChi());
              cals.setString(4, kh.getDienThoai());
               cals.setDate(5, new java.sql.Date(kh.getNgaySinh().getTime()));
               cals.setString(6, kh.getUserName());
               cals.setString(7, kh.getPassWord());
              
               
               cals.execute();
               
        } catch (SQLException ex) {
            return false;
            
        }
        finally{
        CloseConnect();
        }
       
        return true;
    }
    public void CloseConnect()
    {
        try{
        connect.Close();
        }catch(Exception exc)
        {
        
        }
    }
    public String XoaKhachHang(int makh)
    {
        String Message="";
        Connection conn=connect.GetConnect();
        try {
            CallableStatement cals=conn.prepareCall("{call sp_XoaKhachHang(?,?)}");
            cals.setInt(1, makh);
            cals.setString(2, "");
            cals.registerOutParameter(2, java.sql.Types.NVARCHAR);
            
            cals.execute();
               Message=cals.getString(2);
                 
               
               
        } catch (SQLException ex) {
          Message="Faild";
            System.out.println(ex.getMessage());
        }
        finally{
            CloseConnect();
        }
       return Message;
        
    }
     public CallableStatement DanhSachKhachHang()
    {
        
        
        Connection conn=connect.GetConnect();
        ResultSet result=null;
        CallableStatement cals=null;
        try {
             cals=conn.prepareCall("{call sp_ThongTinKhachHang(?)}");
              return cals;
        }
        catch(Exception e)
        {
        }
        return cals;
    }
     
     public KhachHang Search1KhachHang(int makh)
      {
          KhachHang kh=new KhachHang();
          Connection conn=connect.GetConnect();
          try
          {
              CallableStatement cals=conn.prepareCall("{call ThongTin1KhachHang(?)}");
              cals.setInt(1, makh);
              ResultSet rs=cals.executeQuery();
              if(rs.next())
              {
              kh.setDiaChi(rs.getString("DiaChi"));
              kh.setDienThoai(rs.getString("DienThoai"));
              kh.setMaKh(rs.getInt("Ma_KH"));
              kh.setPassWord(rs.getString("PassWord"));
              kh.setTen(rs.getString("Ten_KH"));
              kh.setTinhTrang(rs.getString("TinhTrang"));
              kh.setUserName(rs.getString("UserName"));
              kh.setNgaySinh(rs.getDate("NgaySinh"));
              
              }
              rs.close();
          }catch(Exception e)
          {
              return null;
          }
          finally{
              CloseConnect();
          }
          return kh;
      }
    
}
